unit MultyFilterUtils;

interface
type
    TSelectedFilterValueRecord = record
        Value: string;
        FilterName: string;
    end;

    TSelectedFilterValueArray = array of TSelectedFilterValueRecord;

    TSelectedFilterValue = class
    private
        arr: TSelectedFilterValueArray;
    public
        function AddNewValue(FilterName: string): integer;
        function GetFilterValue(FilterName: string): string;
        procedure SetValue(index: integer; Value: string);
        function GetRecord(index: integer): TSelectedFilterValueRecord;
        constructor Create();
        function Count(): integer;
        procedure Assign(arr_in: TSelectedFilterValue);
    end;

implementation

{ TSelectedFilterValue }

function TSelectedFilterValue.AddNewValue(FilterName: string): integer;
var rec: TSelectedFilterValueRecord;
begin
    Result := Count();
    SetLength(arr, Result + 1);
    rec.FilterName := FilterName;
    rec.Value := '';
    arr[Result] := rec;
end;

procedure TSelectedFilterValue.Assign(arr_in: TSelectedFilterValue);
var i, ind: integer;
    rec: TSelectedFilterValueRecord;
begin
    SetLength(arr, 0);
    if arr_in = nil then Exit;
    for i := 0 to arr_in.Count() - 1 do begin
        rec := arr_in.GetRecord(i);
        ind := AddNewValue(rec.FilterName);
        SetValue(ind, rec.Value);
    end;
end;

function TSelectedFilterValue.Count: integer;
begin
    result := Length(arr);
end;

constructor TSelectedFilterValue.Create;
begin
    SetLength(arr, 0);
end;

function TSelectedFilterValue.GetFilterValue(FilterName: string): string;
var i: integer;
begin
    Result := '';
    for i := Low(arr) to High(arr) do
        if arr[i].FilterName = FilterName then begin
            Result := arr[i].Value;
            Break;
        end;
end;

function TSelectedFilterValue.GetRecord(
  index: integer): TSelectedFilterValueRecord;
begin
    Result := arr[index];
end;

procedure TSelectedFilterValue.SetValue(index: integer; Value: string);
begin
    Assert((index >= 0) and (index < Count()));
    arr[index].Value := Value;
end;

end.

